package action.info;

import javax.servlet.http.HttpServletRequest;

import net.fckeditor.FCKeditor;

import org.apache.struts2.interceptor.ServletRequestAware;

import service.DocService;

import com.opensymphony.xwork2.ActionSupport;

public class DocPublishAction extends ActionSupport implements ServletRequestAware {

    private static final long serialVersionUID = 8876666024069735597L;

    private HttpServletRequest request;

    private DocService docService;

    private String editorHtml;

    private String editorInstanceName;

    private String docTitle;

    private String docContent;

    private String docId;

    private String publishState;

    public String preparePublish() throws Exception {

        editorInstanceName = "docEditor";

        FCKeditor editor = new FCKeditor(request, "docEditor");
        editor.setToolbarSet("Doc");
        editor.setHeight("400px");

        editorHtml = editor.createHtml();

        return SUCCESS;
    }

    public String publish() throws Exception {

        if (docTitle == null || "".equals(docTitle)) {
            publishState = "null title";
        } else if (docContent == null || "".equals(docContent)) {
            publishState = "null content";
        } else {
            docService.saveDoc(docTitle, docContent);
            publishState = "success";
        }
        return SUCCESS;
    }

    public String rePublish() throws Exception {
        if (docTitle == null || "".equals(docTitle)) {
            publishState = "null title";
        } else if (docContent == null || "".equals(docContent)) {
            publishState = "null content";
        } else {
            docService.updateDoc(docId, docTitle, docContent);
            publishState = "success";
        }
        return SUCCESS;
    }

    public String getEditorHtml() {
        return editorHtml;
    }

    public String getEditorInstanceName() {
        return editorInstanceName;
    }

    public void setDocTitle(String docTitle) {
        this.docTitle = docTitle;
    }

    public void setDocContent(String docContent) {
        this.docContent = docContent;
    }

    public void setDocId(String docId) {
        this.docId = docId;
    }

    public String getPublishState() {
        return publishState;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setDocService(DocService docService) {
        this.docService = docService;
    }

}
